﻿#ifndef _CELL_MSG_STREAM_HPP_
#define _CELL_MSG_STREAM_HPP_

#include "CELLStream.hpp"
#include "MessageHeader.hpp"
#include <string>
#include <cstring>

/*
	消息接收数据字节流
*/
class CELLReadMsgStream : public CELLStream
{
public:
	CELLReadMsgStream(DataHeader* header)
		: CELLStream((char*)header, header->dataLength, false)
	{
		push(header->dataLength);
		// 预读取网络消息长度
		ReadInt16();
		// 预先读取消息命令
		getNetCmd();
	}

	CELLReadMsgStream(char* pData, int nSize, bool bDelete = false)
		: CELLStream(pData, nSize, bDelete)
	{
		push(nSize);
		// 预读取网络消息长度
		ReadInt16();
		// 预先读取消息命令
		getNetCmd();
	}
	
	uint16_t getNetCmd()
	{
		uint16_t cmd = CMD_ERROR;
		Read<uint16_t>(cmd);
		return cmd;
	}
};
/*
	消息发送数据字节流
*/
class CELLWriteMsgStream : public CELLStream
{
public:
	CELLWriteMsgStream(char* pData, int nSize, bool bDel = false)
		: CELLStream(pData, nSize, bDel)
	{
		// 预先占领消息长度数据空间
		Write<uint16_t>(0);
	}

	CELLWriteMsgStream(int nSize = 1024)
		: CELLStream(nSize)
	{
		// 预先占领消息长度数据空间
		Write<uint16_t>(0);
	}

	void setNetCmd(uint16_t cmd)
	{
		Write<uint16_t>(cmd);
	}

	bool WriteString(const char* str, int len)
	{
		return WriteArray(str, len);
	}

	bool WriteString(const char* str)
	{
		return WriteArray(str, strlen(str));
	}

	bool WriteString(std::string& str)
	{
		return WriteArray(str.c_str(), str.size());
	}

	void finish()
	{
		// 把 预先占领消息长度数据空间的值 赋为 本次消息的长度
		int curWritePos = length();
		setWritePos(0);
		Write<uint16_t>(curWritePos);
		setWritePos(curWritePos);
	}
};

#endif // #ifndef _CELL_MSG_STREAM_HPP_
